<template>
  <el-form
    ref="userForm"
    :rules="rules"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="updateInfo">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserStaffPhoto } from '@/api/user'
import { updateUseInfo } from '@/api/employees '
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.loadLoginSet()
  },
  methods: {
    async loadLoginSet() {
      try {
        const res = await getUserStaffPhoto(this.$route.query.id)
        // console.log(this.$route.query.id)
        // console.log(res)
        this.userInfo = res.data
        // this.userInfo.username = res.data.username
        // this.userInfo.password = res.data.password
      } catch (e) {
        console.log(e)
      }
    },
    async doUpdate() {
      try {
        const res = await updateUseInfo(this.userInfo)
        // console.log(res)
        this.$message.success(res.message)
        this.$router.back()
      } catch (e) {
        this.$message.success(e.message)
      }
    },
    async updateInfo() {
      const valid = await this.$refs.userForm.validate().catch(e => e)
      // console.log(this.$refs.userForm)
      // console.log(valid)
      if (!valid) return
      this.doUpdate()
    }
  }

}
</script>

